<template>
  <div>
    <!--     <el-button  type="primary" >新增</el-button>-->
    <el-form :inline="true">
      <el-form-item>
        <el-button type="primary" @click="dialogVisible = true">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table
        :data="tableData"
        style="width: 100%;margin-bottom: 20px;"
        stripe
        row-key="id"
        border
        default-expand-all
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
      <!--       名称-->
      <el-table-column
          prop="name"
          label="名称"
      >
      </el-table-column>
      <!--       权限编码-->
      <el-table-column
          prop="perms"
          label="权限编码"
      >
      </el-table-column>
      <!--       图标-->
      <el-table-column
          prop="icon"
          label="图标">
      </el-table-column>
      <!--       类型-->
      <el-table-column
          prop="type"
          label="类型">
        <template slot-scope="scope">
          <el-tag size="small" v-if="scope.row.type === 0">目录</el-tag>
          <el-tag size="small" v-else-if="scope.row.type === 1" type="success">菜单</el-tag>
          <el-tag size="small" v-else="scope.row.type === 2" type="info">按钮</el-tag>
        </template>
      </el-table-column>
      <!--       菜单URL-->
      <el-table-column
          prop="path"
          label="菜单URL"
      >
      </el-table-column>
      <!--       菜单组件-->
      <el-table-column
          prop="component"
          label="菜单组件"
      >
      </el-table-column>
      <!--       排序号-->
      <el-table-column
          prop="orderNum"
          label="排序号"
      >
      </el-table-column>
      <!--       状态-->
      <el-table-column
          prop="date"
          label="状态"
      >
        <template slot-scope="scope">
          <el-tag size="small" v-if="scope.row.statu === 1" type="success">正常</el-tag>
          <el-tag size="small" v-else="scope.row.statu === 0" type="danger">禁用</el-tag>
        </template>
      </el-table-column>
      <!--       操作-->
      <el-table-column
          prop="date"
          label="操作"
      >
        <template slot-scope="scope">
          <el-button type="text"  @click="editHandle(scope.row.id)">编辑</el-button>
          <el-divider direction="vertical"></el-divider>
          <template>
            <el-popconfirm title="这是一段内容确定删除吗？" @confirm = delHandle(scope.row.id)>
              <el-button slot="reference" type="text" > 删除</el-button>
            </el-popconfirm>
          </template>
        </template>
      </el-table-column>
    </el-table>

    <!--     新增弹窗-->
    <el-dialog
        title="提示"
        :visible.sync="dialogVisible"
        width="600px"
        :before-close = "handleClose"
    >
      <el-form :model="editForm" :rules="editFromRules" ref="editForm" label-width="100px" class="demo-editForm">
        <!--  上级菜单-->
        <el-form-item label="上级菜单" prop="parentId">
          <el-select v-model="editForm.parentId" placeholder="请选择上级菜单">
            <!--            第一次循环的是外围的菜单名称-->
            <template v-for="item in tableData">
              <!-- :label="item.name" -->
              <el-option  :value="item.id"  :label="item.name" >
                
              </el-option>
              <!--              第二次循环的是外围菜单名称的孩子，需要去判断一下这个type，当type 等于目录的时候需要获取，当为按钮的时候，不能获取到-->
              <template v-for="child in item.children">
                <el-option :label="child.name" :value="child.id">
                  <span>{{ "- " + child.name }}</span>
                </el-option>
              </template>
            </template>
          </el-select>
        </el-form-item>
        <!--        菜单名称-->
        <el-form-item label="菜单名称" prop="name">
          <el-input v-model="editForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <!--        权限编码-->
        <el-form-item label="权限编码" prop="perms">
          <el-input v-model="editForm.perms" autocomplete="off"></el-input>
        </el-form-item>
        <!--        图标-->
        <el-form-item label="图标" prop="icon">
          <el-input v-model="editForm.icon" autocomplete="off"></el-input>
        </el-form-item>
        <!--        菜单url-->
        <el-form-item label="菜单URL" prop="path">
          <el-input v-model="editForm.path" autocomplete="off"></el-input>
        </el-form-item>
        <!--        菜单组件-->
        <el-form-item label="菜单组件" prop="component">
          <el-input v-model="editForm.component" autocomplete="off"></el-input>
        </el-form-item>
        <!--        类型-->
        <el-form-item label="类型" prop="type">
          <el-radio-group v-model="editForm.type">
            <el-radio :label=0>目录</el-radio>
            <el-radio :label=1>菜单</el-radio>
            <el-radio :label=2>按钮</el-radio>
          </el-radio-group>
        </el-form-item>
        <!--        状态-->
        <el-form-item label="状态" prop="statu">
          <el-radio-group v-model="editForm.statu">
            <el-radio :label=0>禁用</el-radio>
            <el-radio :label=1>正常</el-radio>
          </el-radio-group>
        </el-form-item>
        <!--        排序号-->
        <el-form-item label="排序号" prop="orderNum">
          <el-input v-model="editForm.orderNum">
            <el-input-number v-model="editForm.orderNum" :min="1" label="排序号">1</el-input-number>
          </el-input>
        </el-form-item>
        <el-form-item style="margin-left: 200px">
          <el-button type="primary" @click="submitForm('editForm')">立即创建</el-button>
          <el-button @click="resetForm('editForm')">重置</el-button>
        </el-form-item>
      </el-form>
      <!--    <el-button @click="dialogVisible = false">取 消</el-button>-->
      <!--    <el-button type="primary" @click="dialogVisible = false">确 定</el-button>-->

    </el-dialog>
  </div>
</template>


<script>
export default {
  name: "Menu",
  created() {
    this.getMenuTree()
  },
  data() {
    return {
      dialogVisible: false,
      editForm: {},
      editFromRules: {
        parentId: [
          {required: true, message: '请输入活动名称', trigger: 'blur'},
        ],
        name: [
          {required: true, message: '请选择上级菜单', trigger: 'blur'},
        ],
        perms: [
          {required: true, message: '请输入名称', trigger: 'blur'},
        ],
        type: [
          {required: true, message: '请输入权限编码', trigger: 'blur'},
        ],
        orderNum: [
          {required: true, message: '请选择排序号', trigger: 'blur'},
        ],
        statu: [
          {required: true, message: '请选择状态', trigger: 'blur'},
        ]
      },
      tableData: [],
    }
  },
  methods: {
    load(tree, treeNode, resolve) {
      setTimeout(() => {
        resolve([
          {
            id: 31,
            date: '2016-05-01',
            name: '王小虎',
            address: '上海市普陀区金沙江路 1519 弄'
          }, {
            id: 32,
            date: '2016-05-01',
            name: '王小虎',
            address: '上海市普陀区金沙江路 1519 弄'
          }
        ])
      }, 1000)
    },
    handleClose(){
      this.resetForm('editForm')
    },
    getMenuTree() {
      this.$axios.get("/sys/menu/list").then(res => {
        this.tableData = res.data.data
      })
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$axios.post("/sys/menu/" + (this.editForm.id ? "update" : "save"), this.editForm).then(res => {
            this.$message({
              showClose: true,
              message: '恭喜你，操作成功',
              type: 'success',
              onClose: () => {
                this.getMenuTree()
              }
            });

          })
          this.dialogVisible = false
        } else {
          this.$message.error('错了哦，这是一条错误消息');
        }
      });
    },
     editHandle(id) {
     // localStorage.setItem("menuId",id)
      //当点击编辑的时候得显示这个弹窗
      debugger
     
      console.log(this.editForm);
      this.$axios.get("/sys/menu/info/"+id).then(res =>{
        this.dialogVisible = true;
          if(res.data.data.parentId !== 0 ){
            this.editForm = res.data.data
          }else{
            this.editForm = res.data.data
            this.tableData.forEach(e => {
              if(e.id === id){
                this.editForm.parentId = e.name
              }
              // console.log(this.editForm.parentId)
          })
        }

     
        

        console.log(res.data.data);
      })
    },
    resetForm(formName){
      this.$refs[formName].resetFields();
      this.dialogVisible = false
      this.editForm = {}
    },
    delHandle(id){
      debugger
      this.$axios.post("/sys/menu/delete/"+id).then(res =>{
        this.getMenuTree()
      })
    }

  },
}
</script>


<style scoped>
.el-table {
  margin: 10px;
}
</style>